<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.companyName" clearable class="filter-item" style="width: 160px;" placeholder="请输入企业名" />
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
      <el-button :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>

      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" class="table-expand">
            <el-form-item label="ID">
              <span>{{ props.row.id }}</span>
            </el-form-item>
            <el-form-item label="企业ID">
              <span>{{ props.row.companyId }}</span>
            </el-form-item>
            <el-form-item label="企业名">
              <span>{{ props.row.companyName }}</span>
            </el-form-item>
            <el-form-item label="兑换码">
              <span>{{ props.row.code }}</span>
            </el-form-item>
            <el-form-item label="兑换状态">
              <span>{{ !props.row.status ? '未兑换' : '已兑换' }}</span>
            </el-form-item>

          </el-form>
        </template>
      </el-table-column>

      <el-table-column align="center" label="ID" prop="id" />
      <el-table-column align="center" label="企业ID" prop="companyId" />
      <el-table-column align="center" min-width="100" label="企业名" prop="companyName" />

      <el-table-column align="center" label="兑换码" prop="code" />
      <el-table-column align="center" label="兑换状态" :formatter="statusFormatter" prop="status" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <el-tooltip placement="top" content="返回顶部">
      <back-to-top :visibility-height="100" />
    </el-tooltip>

  </div>
</template>

<style>
  .table-expand {
    font-size: 0;
  }
  .table-expand label {
    width: 100px;
    color: #99a9bf;
  }
  .table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
  }
  .gallery {
    width: 80px;
    margin-right: 10px;
  }
  .company-detail-box img {
    width: 100%;
  }
</style>

<script>
import { listCdkey } from '@/api/cdkey'
import BackToTop from '@/components/BackToTop'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'CdkeyList',
  components: { BackToTop, Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined,
        sort: 'add_time',
        order: 'desc'
      },
      detailDialogVisible: false,
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      listCdkey(this.listQuery).then(response => {
        this.list = response.data.data.list
        this.total = response.data.data.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 禁用状态格式化
    statusFormatter(row, column) {
      const status = row.status
      if (!status) {
        return '未兑换'
      } else {
        return '已兑换'
      }
    },
    handleDownload() {
      this.downloadLoading = true
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['ID', '企业ID', '企业名', '兑换码', '兑换状态']
          const filterVal = ['id', 'companyId', 'companyName', 'code', 'status']
          excel.export_json_to_excel2(tHeader, this.list, filterVal, '兑换码')
          this.downloadLoading = false
        })
    }
  }
}
</script>
